<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Tom的博客">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        linux下使用mdadm组软raid - Tom的博客
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/aircloud.css">

    
<link rel="stylesheet" href="/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 间歇性壮志凌云, 持续性混吃等死 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Tom</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%BD%AFraid"><span class="toc-text">创建软raid</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E6%8B%9F%E6%95%B0%E6%8D%AE%E7%9B%98%E6%8D%9F%E5%9D%8F"><span class="toc-text">模拟数据盘损坏</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E6%8B%9F%E7%B3%BB%E7%BB%9F%E7%9B%98%E6%8D%9F%E5%9D%8F"><span class="toc-text">模拟系统盘损坏</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8D%9F%E5%9D%8F%E9%80%9A%E7%9F%A5"><span class="toc-text">损坏通知</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 间歇性壮志凌云, 持续性混吃等死 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        linux下使用mdadm组软raid
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-08-12 23:04:14</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#linux" title="linux">linux</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <p>为了给自己组一台nas，保护里面数据的安全，所以查阅了挺多资料，终于在虚拟机上试验成功了软raid的一些操作，写篇文章记录一下。</p>
<h4 id="创建软raid"><a href="#创建软raid" class="headerlink" title="创建软raid"></a>创建软raid</h4><p>我是想创建一个raid5，raid5至少需要3块硬盘，现在主流的nas上应该都有4个硬盘位（除了系统盘之外）。<br>在创建之前需要将所有的数据盘分区，一个硬盘一个区。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">sudo fdisk /dev/sdb</span><br><span class="line"><span class="comment"># Welcome to fdisk (util-linux 2.31.1).</span></span><br><span class="line"><span class="comment">#Changes will remain in memory only, until you decide to write them.</span></span><br><span class="line"><span class="comment">#Be careful before using the write command.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Command (m for help): d</span></span><br><span class="line"><span class="comment">#Selected partition 1</span></span><br><span class="line"><span class="comment">#Partition 1 has been deleted.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Command (m for help): n</span></span><br><span class="line"><span class="comment">#Partition type</span></span><br><span class="line"><span class="comment">#p   primary (0 primary, 0 extended, 4 free)</span></span><br><span class="line"><span class="comment">#e   extended (container for logical partitions)</span></span><br><span class="line"><span class="comment">#Select (default p): </span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Using default response p.</span></span><br><span class="line"><span class="comment">#Partition number (1-4, default 1): </span></span><br><span class="line"><span class="comment">#First sector (2048-10485759, default 2048): </span></span><br><span class="line"><span class="comment">#Last sector, +sectors or +size&#123;K,M,G,T,P&#125; (2048-10485759, default 10485759): </span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Created a new partition 1 of type &#x27;Linux&#x27; and of size 5 GiB.</span></span><br><span class="line"><span class="comment">#Partition #1 contains a linux_raid_member signature.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Do you want to remove the signature? [Y]es/[N]o: Y</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#The signature will be removed by a write command.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#Command (m for help): w</span></span><br><span class="line"><span class="comment">#The partition table has been altered.</span></span><br><span class="line"><span class="comment">#Syncing disks.</span></span><br><span class="line"></span><br><span class="line">sudo fdisk /dev/sdc</span><br><span class="line">sudo fdisk /dev/sdd</span><br><span class="line">sudo fdisk /dev/sde</span><br></pre></td></tr></table></figure>
<p>分好区之后就可以开始创建软raid了</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mdadm -C /dev/md0 -a yes -l 5 -n 4 /dev/sdb /dev/sdc /dev/sdd /dev/sde</span><br></pre></td></tr></table></figure>
<p>选项解读：</p>
<ul>
<li>-C 创建</li>
<li>-l 级别</li>
<li>-n 磁盘个数</li>
<li>-a {yes|no} 自动为其创建设备文件</li>
<li>-x 指定空闲盘个数，空闲盘可以在工作盘损坏后自动顶替<br>注意：创建阵列时，阵列所需磁盘数为-n和-x参数的个数和</li>
</ul>
<p>创建完成之后我们就可以开始对磁盘进行格式化，并将配置写入文件</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 格式化</span></span><br><span class="line">sudo mkfs.ext4 /dev/md0</span><br><span class="line"></span><br><span class="line"><span class="comment"># 写入配置文件</span></span><br><span class="line">sudo mdadm -Ds /dev/md0 &gt; mdadm.conf</span><br><span class="line">sudo mv mdadm.conf /etc/</span><br></pre></td></tr></table></figure>
<p>我们还可以查看磁盘状态</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mdadm -D /dev/md0</span><br></pre></td></tr></table></figure>
<p><img src="../img/2019-08-12-23-41-43.png" alt=""></p>
<p>然后我们需要重启，重启完成之后，md0可能会变，我的变成了md127，我也不知道这是为什么,但是我们可以使用<code>sudo fdisk -l</code>来进行查看，查看之后就可以使用<code>mount</code>命令或者编辑<em>/etc/fstab</em>对这个raid后的磁盘进行挂载使用了。</p>
<h4 id="模拟数据盘损坏"><a href="#模拟数据盘损坏" class="headerlink" title="模拟数据盘损坏"></a>模拟数据盘损坏</h4><p>我在实验之前已经将所有的磁盘都设置为了可以热插拔，所以在虚拟机运行的时候我也可以移除磁盘。现在我们要模拟损坏，然后如何替换新的磁盘上去，我们先在虚拟机上将其中一个数据盘移除。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mdadm -D /dev/md127</span><br></pre></td></tr></table></figure>
<p><img src="../img/2019-08-12-23-43-39.png" alt=""></p>
<p>现在我们可以看到Active Devices和Working Devices都已经变成了3，最下面的一个磁盘也已经显示removed。然后我使用虚拟机在原来的位置插入一个新的磁盘，模拟我们替换上新磁盘。</p>
<p>使用<code>sudo fdisk -l</code>可以看到，我们的新磁盘已经被认到了，现在要对这个新硬盘进行分区，分区时需要使用<strong>t</strong>将分区类型选择为fd</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将新硬盘添加到原有raid中</span></span><br><span class="line">sudo mdadm /dev/md127 -a /dev/sde1</span><br><span class="line"><span class="comment"># 现在再查看raid的状态</span></span><br><span class="line">sudo mdadm -D /dev/md127</span><br></pre></td></tr></table></figure>
<p><img src="../img/2019-08-12-23-52-10.png" alt=""></p>
<p>现在我们可以看到新硬盘已经加入raid，并且已经在重建数据了，时间可能比较久，但是不影响系统运行。</p>
<h4 id="模拟系统盘损坏"><a href="#模拟系统盘损坏" class="headerlink" title="模拟系统盘损坏"></a>模拟系统盘损坏</h4><p>我们在使用过程中，不光会出现数据盘损坏的情况，也有可能出现系统盘损坏的情况，当系统盘损坏的时候，我们如何让一个新系统识别已有的raid组合。</p>
<p>我在虚拟机上重新安装了系统开机后，发现系统已经直接认到了raid盘，也就是说什么都不用做。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们主动解除raid组合</span></span><br><span class="line">sudo mdadm -S /dev/md0</span><br></pre></td></tr></table></figure>
<p>当我们主动解除raid盘之后，系统就已经不能认到raid了，这个时候，我们可以使用以下命令再次识别raid,也可以直接将之前的<code>mdadm.conf</code>直接复制到<em>/etc/</em>目录，重启之后，他就会自动帮我们识别好raid了。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mdadm -A /dev/md0 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1</span><br></pre></td></tr></table></figure>
<h4 id="损坏通知"><a href="#损坏通知" class="headerlink" title="损坏通知"></a>损坏通知</h4><p>我们不可能每天都去查看硬盘有没有损坏，这样非常麻烦，所以我自己写了一个脚本，实现监控硬盘的功能，当它检测到硬盘损坏时，就会发一封邮件通知你。首先要让自己的nas拥有发邮件的功能，邮件的配置请参考我的<a target="_blank" rel="noopener" href="https://yaozhijin.gitee.io/Linux%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%91%E9%82%AE%E4%BB%B6.html">这篇博客</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#! /usr/bin/python3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line">	result = os.popen(<span class="string">&#x27;mdadm -D /dev/md0&#x27;</span>)</span><br><span class="line">	res = result.read()</span><br><span class="line">	active = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> line <span class="keyword">in</span> res.splitlines():</span><br><span class="line">		<span class="keyword">if</span> <span class="string">&#x27;Active Devices : 4&#x27;</span> <span class="keyword">in</span> line:</span><br><span class="line">			active = <span class="number">1</span></span><br><span class="line">			<span class="keyword">break</span></span><br><span class="line">		<span class="keyword">else</span>:</span><br><span class="line">			<span class="keyword">continue</span></span><br><span class="line">		active = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> active == <span class="number">1</span>:</span><br><span class="line">		time.sleep(<span class="number">600</span>)</span><br><span class="line">	<span class="keyword">else</span>:</span><br><span class="line">		os.system(<span class="string">&quot;echo &#x27;你的raid可能出现了一些问题，请查看一下&#x27; | mutt -s &#x27;raid问题报告&#x27; pi_sender@163.com&quot;</span>)</span><br><span class="line">		time.sleep(<span class="number">86400</span>)</span><br></pre></td></tr></table></figure>
<p>最后在终端执行<code>sudo ./raid-monitor.py</code>就好了</p>
<p>参考链接：<a target="_blank" rel="noopener" href="http://www.toxingwang.com/linux-unix/linux-basic/1048.html">http://www.toxingwang.com/linux-unix/linux-basic/1048.html</a></p>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
